package avortep.model;

import java.util.ArrayList;
import java.util.List;


public class DBTable extends DBAbstractTable {
	DBPrimaryKey primaryKey;
	List<DBForeignKey> foreignKeys;
	
	public DBTable(String name) {
		super(name);
		primaryKey = new DBPrimaryKey("PK_"+this.getName());
		foreignKeys = new ArrayList<DBForeignKey>();
	}

	public void addPKColumn(DBColumn column){
		if (this.hasChild(column))
			if (!primaryKey.hasChild(column)){
				primaryKey.addColumn(column);
			}				
	}
	
	public void addFKColumn(DBColumn column, DBColumn parentColumn){
		if (this.hasChild(column) && !this.hasChild(parentColumn)){
			DBForeignKey fk = new DBForeignKey("FK_"+this.getName()+"."+column.getName()+"_REF_"+parentColumn.getName());
			fk.setColumns(column, parentColumn);
			foreignKeys.add(fk);
		}
	}
	
	public DBForeignKey getFKForColumn(DBColumn column) {
		for (DBForeignKey fk:foreignKeys)
			if (fk.column==column)
				return fk;
		return null;
	}

	public void setPrimaryKeyName(String primaryKeyName) {
		if (primaryKey!=null){
			primaryKey.setName(primaryKeyName);
		}
	}

	protected DBPrimaryKey getPrimaryKey() {
		return primaryKey;
	}

}
